<?php
namespace submodule\modules\admin\components;

use Yii;
use yii\db\Schema;
use yii\db\Migration;

class Uninstaller extends Migration
{
	public $controller;
	
	public function __construct($controller){
		$this->controller = $controller;
		
	}
    //安装
    public function install($option)
    {
		//建表
        Yii::$app->db->createCommand()->createTable('{{%'.$option['tablename'].'}}', [
			'id'=> "pk",
			'username'=> "varchar(255) NOT NULL",
			'auth_key'=> "varchar(32) NOT NULL",
			'password_hash'=> "varchar(255) NOT NULL",
			'password_reset_token'=> "varchar(255) default NULL",
			'email'=> "varchar(255) NOT NULL",
			'role'=> "smallint(6) NOT NULL default '10'",
			'status'=> "smallint(6) NOT NULL default '10'",
			'created_at'=> "int(11) NOT NULL",
			'updated_at'=> "int(11) NOT NULL",
        ],'ENGINE InnoDB DEFAULT CHARSET utf8')->execute();
		


		
		//记录字段
		$fields = ['modelid', 'name', 'field', 'formtype', 'fieldtype', 'fieldlength', 'listorder', 'disabled', 'primarytable', 'required', 'setting', 'param', 'api', 'allowinput'];

		
		$rows = [
		     [$option['modelid'], 'ID', 'id', 'primarykey', 'int', 10, 0, 0, 1, 1, '', '', '', 0], 
			 [$option['modelid'], '登录帐号', 'username', 'text', 'varchar', 255, 0, 0, 1, 1, '', '', '', 1], 
			 [$option['modelid'], 'auth_key', 'auth_key', 'text', 'varchar', 32, 0, 0, 1, 1, '', '', '', 0], 
			 [$option['modelid'], 'password_hash', 'password_hash', 'text', 'varchar', 255, 0, 0, 1, 1, '{"defaultvalue":"0"}', '', '', 0], 
			 [$option['modelid'], 'password_reset_token', 'password_reset_token', 'text', 'varchar', 255, 0, 0, 1, 1, '', '', '', 0], 
			 [$option['modelid'], '邮箱', 'email', 'text', 'varchar', 255, 0, 0, 1, 1, '', '', '', 1], 
			 [$option['modelid'], '角色', 'role', 'text', 'int', 10, 0, 0, 1, 1, '{"defaultvalue":"10"}', '', '', 0], 
			 [$option['modelid'], '状态', 'status', 'switch', 'int', 10, 0, 0, 1, 0, '{"defaultvalue":"1"}', '', '', 1],
			 [$option['modelid'], '添加时间', 'created_at', 'text', 'int', 10, 0, 0, 1, 0, '', '', '', 0],
			 [$option['modelid'], '更新时间', 'updated_at', 'text', 'int', 10, 0, 0, 1, 0, '', '', '', 0],
			 
	    ];
		Yii::$app->db->createCommand()->batchInsert($option['field_tablename'], $fields, $rows)->execute();
		return [$option['modelid']];
		
    }
	
	
	public function create($option){
		return [$option['modelid']];
	}
    
	
	//卸载
    public function uninstall()
    {
        $this->dropTable('{{store_product}}');
    }
}
